//
//    Copyright (C) 2012  OinkStudio
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package graphics;

import java.util.Random;

import game.Game;
import game.Obolo;
import com.marakana.obolo.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.drawable.Drawable;
import android.view.SurfaceView;
import android.view.View;

public class OboloGraphicsView extends View {
	private Obolo obolo;
	private Context context;
	private final int skillPadding = 3;
	private int oboloWidth = 40;
	private int oboloHeight = 60;

	public OboloGraphicsView(Context context, Obolo obolo) {
		super(context);
		// Referencia del obolo dins de Game
		this.obolo = obolo;
		this.context = context;

	}

	@Override
	protected void onMeasure(int widthSpec, int heightSpec) {
		super.onMeasure(widthSpec, heightSpec);
		// Transforma les mesures especificades per el contenidor o layout en
		// Pixels
		int measuredWidth = MeasureSpec.getSize(widthSpec);
		int measuredHeight = MeasureSpec.getSize(heightSpec);

		// Redueix el tamany del view dins del seu contenidor
		if (measuredWidth > oboloWidth || measuredWidth == 0)
			measuredWidth = oboloWidth;
		if (measuredHeight > oboloHeight || measuredHeight == 0)
			measuredHeight = oboloHeight;

		// Defineix la mesura del view dins del seu contenidor o Layout
		setMeasuredDimension(measuredWidth, measuredHeight);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Drawable ground;
		Drawable skillIcon;

		if (obolo == null) {
			ground = context.getResources().getDrawable(R.drawable.coinvoid);
			ground.setAlpha(200);
			ground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
			ground.draw(canvas);

		} else {
			if (obolo.getOwner() == Game.RIVAL)
				ground = context.getResources().getDrawable(
						R.drawable.coinrival);
			else
				ground = context.getResources()
						.getDrawable(R.drawable.coinuser);

			String skillDraw;
			Paint txtPaint = new Paint();

			// Captura la imatge del fons del obolo
			ground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
			ground.draw(canvas);

			// get the size of your control based on last call to onMeasure
			// Recupera la mida del view basat en la ultima crida que s'ha fet
			// al onMesure
			int height = getMeasuredHeight();
			int width = getMeasuredWidth();

			// Defineix el color del paint
			txtPaint.setColor(Color.BLACK);
			// Defineix que el text sigui en negreta
			txtPaint.setFakeBoldText(true);
			// Defineix com s'alineara el text
			txtPaint.setTextAlign(Align.LEFT);

			// Defineix l'al�ada de la skillNumber
			int skillHeight = oboloHeight / 3;
			// Defineix la variable que guardara l'amplada del skillNumber
			float skillWidth;
			// Defineix la mida de la font que pinta la skillNumber
			txtPaint.setTextSize(skillHeight);
			// Defineix la mida del cuadre on es dibuixaran les demes skills
			int sizeSkill = skillHeight * 3 / 4;

			// Printa el text en el view a les posicions que se li demana i amb
			// el Paint creat

			// Pinta el Nord
			skillDraw = obolo.getNorthName();
			if (skillDraw.length() > 1) {
				// Si es una skill es pinta el drawable
				skillIcon = context.getResources().getDrawable(
						selectSkillIcon(skillDraw));
				skillIcon.setBounds(width / 2 - sizeSkill / 2, skillPadding,
						width / 2 + sizeSkill / 2, sizeSkill + skillPadding);
				skillIcon.draw(canvas);
			} else {
				// Si es una skillNumber es pinta mitjan�ant el paint
				skillWidth = txtPaint.measureText(skillDraw);
				canvas.drawText(skillDraw, width / 2 - skillWidth / 2, txtPaint
						.getTextSize()
						- skillPadding, txtPaint);
			}

			// Pinta el Sud
			skillDraw = obolo.getSouthName();
			if (skillDraw.length() > 1) {
				// Si es una skill es pinta el drawable
				skillIcon = context.getResources().getDrawable(
						selectSkillIcon(skillDraw));
				skillIcon.setBounds(width / 2 - sizeSkill / 2, height
						- sizeSkill - skillPadding, width / 2 + sizeSkill / 2,
						height - skillPadding);
				skillIcon.draw(canvas);
			} else {
				// Si es una skillNumber es pinta mitjan�ant el paint
				skillWidth = txtPaint.measureText(skillDraw);
				canvas.drawText(skillDraw, width / 2 - skillWidth / 2, height
						- skillPadding, txtPaint);
			}

			// Pinta el Oest
			skillDraw = obolo.getWestName();
			if (skillDraw.length() > 1) {
				// Si es una skill es pinta el drawable
				skillIcon = context.getResources().getDrawable(
						selectSkillIcon(skillDraw));
				skillIcon.setBounds(skillPadding, height / 2 - sizeSkill / 2,
						skillPadding + sizeSkill, height / 2 + sizeSkill / 2);
				skillIcon.draw(canvas);
			} else {
				// Si es una skillNumber es pinta mitjan�ant el paint
				skillWidth = txtPaint.measureText(skillDraw);
				canvas.drawText(skillDraw, skillPadding, height / 2
						+ skillHeight / 2 - skillPadding, txtPaint);
			}

			// Pinta el Est
			skillDraw = obolo.getEastName();
			if (skillDraw.length() > 1) {
				// Si es una skill es pinta el drawable
				skillIcon = context.getResources().getDrawable(
						selectSkillIcon(skillDraw));
				skillIcon.setBounds(width - sizeSkill - skillPadding, height
						/ 2 - sizeSkill / 2, width - skillPadding, height / 2
						+ sizeSkill / 2);
				skillIcon.draw(canvas);
			} else {
				// Si es una skillNumber es pinta mitjan�ant el paint
				skillWidth = txtPaint.measureText(skillDraw);
				canvas.drawText(skillDraw, width - skillWidth - skillPadding,
						height / 2 + skillHeight / 2 - skillPadding, txtPaint);
			}
		}
		setPadding(2, 0, 2, 0);
	}

	private int selectSkillIcon(String skillName) {
		int intReference = R.drawable.icon;
		if (skillName.equals("sword"))
			intReference = R.drawable.sword;
		if (skillName.equals("shield")){
			Random rand = new Random();
			if(rand.nextBoolean())
				intReference = R.drawable.shield;
			else
				intReference = R.drawable.shield2;
		}
		if (skillName.equals("twins"))
			intReference = R.drawable.twins;
		return intReference;
	}

	public Obolo getObolo() {
		return obolo;
	}

	public void setObolo(Obolo obolo) {
		this.obolo = obolo;
	}

	public void setBoundsHeight(int height) {
		// Redimensiona el obolo segons una altura
		int width;
		width = (height * 2) / 3;
		oboloHeight = height;
		oboloWidth = width;
		onMeasure(0, 0);
	}

	public void setBoundsWidth(int width) {
		// Redimensiona el obolo segons una amplada
		int height;
		height = (width * 3) / 2;
		oboloHeight = height;
		oboloWidth = width;
		onMeasure(0, 0);
	}
}
